﻿using System;
using System.Linq;

namespace N2.Workflow
{
	using Items;

	[Obsolete]
	public static class WorkflowPublicExtensions
	{
		/// <summary>
		/// Get Icon for the item depending on it's Workflow state.
		/// Ensure that state is assigned, thus should not throw NullReferenceException.
		/// </summary>
		/// <param name="item"></param>
		/// <returns>String of unresolved URL</returns>
		public static string GetIconFromState(this ContentItem item)
		{
			try {
				var _state = Context.Current.Resolve<IWorkflowProvider>().GetCurrentState(item);

				if (null == _state || null == _state.Definition) {
					return null;
				}
				//TODO fix a cast to concrete type
				return ((ItemState)_state.Definition).IconUrl;
			} catch {
				return null;
			}
		}

		public static bool IsFinalState(this ItemState itemState)
		{
			return !itemState.Definition.Items.Any()
				|| (1 == itemState.Definition.Items.Count()
					&& itemState.Definition.Items.First().LeadsTo == ((Workflow)itemState.Definition.Parent).InitialState
					);
		}

		public static bool IsInitialState(this IItemState itemState)
		{
			IStateDefinition _currentStateDefinition = itemState.Definition;
			
			return
				_currentStateDefinition == _currentStateDefinition.Workflow.InitialState;
		}
	}
}
